package cn.com.eado.crm.app.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.Button;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;
import java.util.List;

import cn.com.eado.crm.R;

import static androidx.core.content.FileProvider.getUriForFile;

public class OtherUtil {
    private static long lastClickTime;
    private final static int SPACE_TIME = 500;
    /**
     * 防止多次点击创建多个页面
     */
    public synchronized static boolean isDoubleClick() {
        long currentTime = System.currentTimeMillis();
        boolean isClick2;
        if (currentTime - lastClickTime >
                SPACE_TIME) {
            isClick2 = false;
        } else {
            isClick2 = true;
        }
        lastClickTime = currentTime;
        return isClick2;
    }



    public static void installApk(Context context, String apkPath) {
        File file = new File(apkPath);
        if (!file.exists()) {
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            checkAndAppPermission((Activity) context, Manifest.permission.REQUEST_INSTALL_PACKAGES);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = getUriForFile(context, context.getApplicationInfo().packageName + ".provider", new File(apkPath));//par2要和manifest.xml的provider一致
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            ((Activity) context).startActivity(intent);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ((Activity) context).startActivity(intent);
        }
//        ActivityManagerUtil.finishAll();
//        android.os.Process.killProcess(android.os.Process.myPid());//exit app
        new Handler().postDelayed(new Runnable() {  //解决华为8.0手机安装apk提示解释包异常
            @Override
            public void run() {
//                System.exit(0);//exit app
                android.os.Process.killProcess(android.os.Process.myPid());//exit app
                LogUtil.e("tag","exit");
            }
        }, 800);
    }

    //判断当前版本是否6.0以上(Build.VERSION_CODES.M=API23),没有权限则申请权限
    public static void checkAndAppPermission(Activity activity, String permission){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED){
                //这里PERMISSION_GRANTED表示具有权限,PERMISSION_DENIED没有权限
                Log.d("tag", "has permission: "+permission);
            }else {
                ActivityCompat.requestPermissions(activity, new String[]{permission}, 101);
            }
        }else {
            //
        }
    }

    public static int getAPNType(Context context) {
        int netType = 0;
        ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = 1;// wifi
        } else if (nType == ConnectivityManager.TYPE_MOBILE) {
            int nSubType = networkInfo.getSubtype();
            TelephonyManager mTelephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                    && !mTelephony.isNetworkRoaming()) {
                netType = 2;// 3G
            } else {
                netType = 3;// 2G
            }
        }
        return netType;
    }

//    public static void enableButton(Context context, Button bt, Boolean enable){
//        bt.setEnabled(enable);
//        if (enable){
//            bt.setBackground(context.getResources().getDrawable(R.drawable.selector_button_bg1));
//        }else{
//            bt.setBackgroundColor(ContextCompat.getColor(context, R.color.colorGray1));
//        }
//    }

    public static SpannableString colorText(String text, List<String> keyword, int color){
        SpannableString ss = new SpannableString(text);
        int temp = 0;
        for (int i=0; i<keyword.size(); i++){
            int start = temp;
            int end = start+keyword.get(i).length();
            temp = end + " → ".length();
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
            ss.setSpan(colorSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

}
